package com.yun.baymax.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.easeui.EaseConstant;
import com.yun.baymax.R;
import com.yun.baymax.adapter.DiscoverAdapter;
import com.yun.baymax.bean.User;
import com.yun.baymax.utils.GlideUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class ProfileActivity extends BaseActivity {
    @BindView(R.id.head_view)
    ImageView headView;
    @BindView(R.id.state)
    TextView state;
    @BindView(R.id.my_location)
    TextView myLocation;
    @BindView(R.id.location_switch)
    Switch switchBar;

    private User user;
    private DiscoverAdapter adapter;
    private String chatUser;
    private Toolbar toolbar;


    @Override
    protected int setLayoutResourceId() {
        return R.layout.activity_profile;
    }

    @Override
    protected void initVariables() {
        ButterKnife.bind(this);
        user = (User) getIntent().getSerializableExtra("user");
        chatUser = getIntent().getStringExtra("chatUser");
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        myLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goNext(null, LocationActivity.class);
            }
        });
    }

    @Override
    protected void loadData() {
        if (user == null) {
            BmobQuery<User> commentQuery = new BmobQuery<>();
            commentQuery.addWhereEqualTo("hxName", chatUser);
            commentQuery.findObjects(new FindListener<User>() {
                @Override
                public void done(List<User> list, BmobException e) {
                    if (e == null) {
                        user = list.get(0);
                        setUpView();
                    }
                }
            });
        } else {
            setUpView();
        }
    }

    private void setUpView() {
        toolbar.setTitle(user.getNick());
        if (!TextUtils.isEmpty(user.getHeadImage()))
            Glide.with(mContext).load(user.getHeadImage()).asBitmap().into(GlideUtils.setCircle(mContext, headView));
        state.setText(user.getContent());

        switchBar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //TODO
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_profile, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.profile_more:
                break;
            case R.id.tool_chat:
                if (user == null) break;
                Intent intent1 = new Intent(mContext, ChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, user.getHxName());
                intent1.putExtra("nick", user.getNick());
                startActivity(intent1);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
